import { apiGet, apiPost, apiPut, apiDelete } from '../api/index'

export const selectRow = (list1, list2, currentRow, oldCurrentRow) => {
  if(currentRow)
  {
    if(currentRow == oldCurrentRow){
      t1.setCurrentRow();
    }
    else
    {
       let row = list1.find(x=>x.ID == currentRow.ID)
       if(row.Positions)
       {
        row.Positions.forEach(element => {
          list2.push(element)
         });
       }
       else
       {
         list2.splice(0)
       }
      }
    
  }
}

export const arrayToIds = objs =>
{
  let ids = []

  if(objs)
  {
    objs.forEach(element => ids.push(element.ID));
  }
  return ids
}

export const confirm = (that, txt)=>{
  return new Promise((resolve,reject)=>{
    that.$confirm(txt, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
      })
      .then(() => { resolve() })
      .catch(()=>{});
  })
}

export const add = (that, url, params, form)=>
{
  return new Promise((resolve,reject)=>{
    form.validate(valide => {
      if (valide) {
        apiPost(url, params).then(res => {
          if (res.code === 200) {
            that.$message({ type: 'success', message: '操作成功！'})
            resolve(res.data)
          }
          else{
            that.$message({ type: 'warning', message: res.message})
            reject()
          }
        })
      }
    })
  })
}
export const edit = (that, url, params, form)=>
{
  return new Promise((resolve,reject)=>{
    form.validate(valide => {
      if (valide) {
        apiPut(url, params).then(res => {
          if (res.code === 200) {
            that.$message({ type: 'success', message: '操作成功！'})
            resolve(res.data)
          }
        })
      }
    })
  })
}
export const editNoValid = (that, url, params)=>
{
  return new Promise((resolve,reject)=>{
        apiPut(url, params).then(res => {
          if (res.code === 200) {
            that.$message({ type: 'success', message: '操作成功！'})
            resolve(res.data)
          }
        })
  })
}
export const deleteBatch = (that,url,objs,txt)=>{
  return new Promise((resolve,reject)=>{
    if(objs.length <=0)
    {
      that.$message({type: 'warning', message: '请选择要删除的记录'})
      return
    }
    confirm(that,"确定要删除所选记录吗？").then(() => { 
      return apiDelete(url, {ids:arrayToIds(objs)})
    })
      .catch(()=>{})
      .then(res => {
        if (res.code === 200) {
          that.$message({ type: 'success', message: '操作成功！'})
          resolve(res.data)
        }})
        .catch(err=> {that.$message({type: 'error', message: err})})
  })
}








export const t1 = (x) => {
  //x.splice(0)
  //x.push({PositionName:"wer"})
  x = [{PositionName:"www"}]
}
